/*

Copyright (c) 2007 Niel M. Bornstein

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

*/

using System;
using System.Collections;
using System.IO;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Xml.Serialization;

namespace Dottlr
{
	
	public class Api : ICertificatePolicy
	{
		public const string baseUri = "https://www.dopplr.com/api/";
	
		private static string token = string.Empty;
		
		private static bool trust = false;

		public static string BaseUri {
			get {
				return baseUri;
			}
		}
		
		public static string Token {
			get {
				return token;
			}
			set {
				token = value;
			}
		}
		
		public static bool IsInitialized {
			get {
				return token != string.Empty;
			}
		}
		
		private Api()
		{
		}
		
		static Api() 
		{
			ServicePointManager.CertificatePolicy = new Api();
		}
		
		public bool CheckValidationResult (ServicePoint sp, 
			X509Certificate certificate, WebRequest request, int error)
		{
			if (error == 0 || trust)
				return true;
			// only ask for trust failure (you may want to handle more cases)
			if (error != -2146762486)
				return false;
	 
			Console.Write ("A trust error occured while attempting to " + 
				"access the Dopplr web site. Do you wish to continue this " +
				"session even though we couldn't assess its security? ");
			trust = Console.ReadLine ().ToLower () == "yes";
			return (trust);
		}

		public static CitySearch DoCitySearch(string name) {
			ValidateToken();
			return CitySearch.DoCitySearch(name);
		}
		
		public static CityInfo DoCityInfo(long geoname_id) {
			ValidateToken();
			return CityInfo.DoCityInfo(geoname_id);
		}
		
		public static AddTrip DoAddTrip(long geoname_id, DateTime start, DateTime end) {
			ValidateToken();
			return AddTrip.DoAddTrip(geoname_id, start, end);
		}
		
		public static AddTripTags DoAddTripTags(long trip_id, string tags) {
			ValidateToken();
			return AddTripTags.DoAddTripTags(trip_id, tags);
		}
		
		public static TripInfo DoTripInfo(long trip_id) {
			ValidateToken();
			return TripInfo.DoTripInfo(trip_id);
		}
		
		public static FutureTripsInfo DoFutureTripsInfo() {
			ValidateToken();
			return FutureTripsInfo.DoFutureTripsInfo();
		}
		
		// Validate the token
		private static void ValidateToken() {
			
			if (token == string.Empty) {
				throw new ArgumentException("No token given");
			}
			
			if (token.Length != 32) {
				throw new ArgumentException("Token not the right length");
			}
		}
	}
}
